////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2009 Julien Etienne.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
////////////////////////////////////////////////////////////////////////////////
package org.siam.impl {
	
	import org.siam.core.ISiamManager;
	import org.siam.core.data.formatting.IFormattingManager;
	import org.siam.core.schemas.ISchema;
	import org.siam.core.schemas.ISchemaManager;
	import org.siam.core.schemas.ISchemaRegistry;
	import org.siam.impl.data.formatting.FormattingManager;
	import org.siam.impl.schemas.Schema;
	import org.siam.impl.schemas.SchemaManager;
	import org.siam.impl.schemas.SchemaRegistry;
	import org.siam.utils.RuntimeError;
	
	/**
	* 
	*
	**/
	[DefaultProperty(name="configuration")]
	public class SiamManager implements ISiamManager {
		
		private var _schemaRegistry:ISchemaRegistry;
		private var _formattingManager:IFormattingManager;
		
		/**
		* 
		*
		**/
		public function SiamManager() {
			_schemaRegistry = new SchemaRegistry();
			_formattingManager = new FormattingManager();
		}

		[Inspectable(category="General")]
		public function set configuration(value:XML):void {
			if (value == null) throw new RuntimeError("XML Configuration is missing!");
			
			parseXML(value);
		}
		
		/**
		* 
		*
		**/
		public function manage(schemaName:String):ISchemaManager {
			var schema:ISchema = _schemaRegistry.find(schemaName);
			return new SchemaManager(this, schema);
		}
		
		/**
		* 
		*
		**/
		public function get schemaNames():Array {
			return _schemaRegistry.names;
		}
		
		/**
		* 
		*
		**/
		public function get formatting():IFormattingManager {
			return _formattingManager;			
		}
		
		private function parseXML(configuration:XML):void {
			if (configuration.name() != "siam-configuration") throw new RuntimeError("");
			
			// Parsing Formatting section
			var formatting:XMLList = configuration.child("formatting");
			_formattingManager = FormattingManager.newInstance(formatting[0]);
			
			// Parsing each mapping
			_schemaRegistry = new SchemaRegistry();
			var mappings:XMLList = configuration.child("mappings");
			for each (var mappingXML:XML in mappings["mapping"]) {
				parseMappingXML(mappingXML);
			}	
		}
		
		private function parseMappingXML(configuration:XML):void {
			for each (var schemaXML:XML in configuration["schema"]) {
				var schema:ISchema = Schema.newInstance(schemaXML);
				_schemaRegistry.register(schema.name, schema);
			}	
		}
	}
}